﻿@model Infrastructure.ExtendedEntities.Store_Product_WithProperties

@{
    ViewBag.Title = "EditProduct";
    Layout = "~/Views/Shared/_Layout_Administration.cshtml";
}
<form>
    <table>

        <tr>
            <td>Name</td>
            <td>
                <input type="text" data-bind="value: self.product.Name" />

            </td>
        </tr>
         <tr>
            <td>Brief description</td>
            <td>
                <input type="text" data-bind="value: self.product.BriefDescription" />

            </td>
        </tr>
        <tr>
            <td>Description</td>
            <td>
                <input type="text" data-bind="value: self.product.Description" />

            </td>
        </tr>
        <tr>
            <td>Cost</td>
            <td>
                <input type="number" data-bind="value: self.product.Cost" />

            </td>
        </tr>
        <tr>
            <td>ArtNo</td>
            <td>
                <input type="text" data-bind="value: self.product.ArtNo" />

            </td>
        </tr>
        <tr>
            <td>Discount</td>
            <td>
                <input type="number" data-bind="value: self.product.Discount" />

            </td>
        </tr>
        <tr>
            <td>ItemsSold</td>
            <td>
                <input type="number" data-bind="value: self.product.ItemsSold" />

            </td>
        </tr>

        <tr>
            <td>IsFreeShiping</td>
            <td>
                <input type="checkbox" data-bind="checked: self.product.IsFreeShiping" />

            </td>
        </tr>
        <tr>
            <td>IsNew</td>
            <td>
                <input type="checkbox" data-bind="checked: self.product.IsNew" />

            </td>
        </tr>
        <tr>
            <td>IsOnSale</td>
            <td>
                <input type="checkbox" data-bind="checked: self.product.IsOnSale" />

            </td>
        </tr>
        <tr>
            <td>IsDownloadable</td>
            <td>
                <input type="checkbox" data-bind="checked: self.product.IsDownloadable" />

            </td>
        </tr>
        <tr>
            <td>IsEnabled</td>
            <td>
                <input type="checkbox" data-bind="checked:  self.product.IsEnabled" />

            </td>
        </tr>

        <tr>
            <td>FriendlyUrl</td>
            <td>
                <input type="text" data-bind="value: self.product.FriendlyUrl" />

            </td>
        </tr>

        <tr>
            <td>SortOrder</td>
            <td>
                <input type="number" data-bind="value: self.product.SortOrder" />

            </td>
        </tr>


        <!-- ko foreach: self.product.CustomProperties  -->
        <tr>
            <td>
                <span data-bind="text: First"></span></td>
            <td>
                <input type="text" data-bind="value: Second" /></td>

        </tr>
        <!-- /ko -->
        ​
  

    </table>
</form>


<button class="btn btn-success" data-bind="click: save">Save all changes</button>

<div>
    <a href="/Administration/Store?categoryId=@Model.Store_Category2Product.FirstOrDefault().Store_Category_Id">Back to category</a>
</div>

@section Scripts {

    <script type="text/javascript">

        var productId = '@ViewBag.productId';
        //var data = '@Html.Raw(ViewBag.Product)';

        function ProductViewModel() {

            self = this;

            self.loadDataFromServer = function (id) {

                $.ajax({
                    type: 'GET',
                    url: "GetProduct?productId=" + productId,
                    dataType: 'json',
                    success: function (allData) {
                        self.product = ko.mapping.fromJS(allData);
                        self.product.IsFromClient = true;
                    },
                    async: false
                });

            };

            self.save = function () {
                $.ajax("SetProduct", {
                    data: ko.toJSON(self.product),
                    type: "post", contentType: "application/json",
                    success: function (result) { alert(result); }

                })
            };

            self.loadDataFromServer();
        }



        ko.applyBindings(new ProductViewModel());

    </script>
    @Scripts.Render("~/bundles/jqueryval")
}
